import { _decorator, AudioSource, Button, Component, Label } from 'cc';
import { Databus } from '../core/Databus';
import { UniformFunction } from '../platform/UniformFunction';
import { RewardedVideoAd } from '../platform/ad/RewardedVideoAd';
import { updateUserGoldCoin } from '../services/UserInfo';
import { updateSetting } from '../services/Setting';

const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('RewordVideoCoin')
export class RewordVideoCoin extends Component {
  private adType = 0; // 0 免费看视频赚金币 1 签到赚金币 2 每日福利赚金币
  private isAdDoubled = true;
  private coinValue = 3000;
  public closeButton: Button = null;
  public freeButton: Button = null;
  public doubleButton: Button = null;
  public tripleButton: Button = null;
  public normalButton: Button = null;
  public adSwitchButton: Button = null;

  public coinLabel: Label = null;
  protected onLoad(): void {
    this.closeButton = this.node.getChildByName('CloseButton').getComponent(Button);
    this.closeButton.node.on(Button.EventType.CLICK, this.closeVideoCoin, this);

    this.freeButton = this.node.getChildByName('FreeRecieveButton').getComponent(Button);
    this.freeButton.node.on(Button.EventType.CLICK, this.watchVideoAd, this);

    this.doubleButton = this.node.getChildByName('DoubleRecieveButton').getComponent(Button);
    this.doubleButton.node.on(Button.EventType.CLICK, this.watchVideoAd, this);

    this.tripleButton = this.node.getChildByName('TripleRecieveButton').getComponent(Button);
    this.tripleButton.node.on(Button.EventType.CLICK, this.watchVideoAd, this);

    this.normalButton = this.node.getChildByName('NormalRecieveButton').getComponent(Button);
    this.normalButton.node.on(Button.EventType.CLICK, this.watchVideoAd, this);

    this.adSwitchButton = this.node.getChildByName('AdSwitch').getChildByName('TogBackground').getComponent(Button);
    this.adSwitchButton.node.on(Button.EventType.CLICK, this.switchVideoAd, this);

    this.coinLabel = this.node.getChildByName('GainCoinLabel').getComponent(Label);
  }

  protected start() {
    this.adType = databus.rad.adType;
    RewardedVideoAd.Instance().create();
    this.isAdDoubled = true;
    this.changeCoin();
  }

  protected update(deltaTime: number) {
    this.adType = databus.rad.adType;
    const a = Math.floor(this.coinValue / 1000);
    this.coinLabel.string = `+${a}, 000`;
    this.changeCoin();
  }
  public switchVideoAd() {
    this.isAdDoubled = !this.isAdDoubled;
    this.changeCoin();
    if (this.isAdDoubled) {
      this.normalButton.node.active = false;
      this.node.getChildByName('AdSwitch').getChildByName('TogButton').active = true;
    } else {
      this.node.getChildByName('AdSwitch').getChildByName('TogButton').active = false;
      this.normalButton.node.active = true;
      this.freeButton.node.active = false;
      this.doubleButton.node.active = false;
      this.tripleButton.node.active = false;
    }
  }
  protected watchVideoAd() {
    if (this.isAdDoubled) {
      RewardedVideoAd.Instance().setCallback(this, this.watchVideoSuss, this.watchVideoFail);
      RewardedVideoAd.Instance().show();
    } else {
      new UniformFunction().showInfoToast('领取成功', 3000);
      this.updateServiceData();
    }
  }
  protected watchVideoSuss() {
    new UniformFunction().showInfoToast('领取成功', 3000);
    this.updateServiceData();
  }
  protected watchVideoFail() {
    new UniformFunction().showInfoToast('领取失败', 3000);
  }
  private closeVideoCoin() {
    this.node.active = false;
  }
  private updateServiceData() {
    if (this.adType === 1) {
      databus.earnCoin.sign += 1;
      this.updateEarnCoin();
    } else if (this.adType === 2) {
      databus.earnCoin.freeTime += 1;
      this.updateEarnCoin();
    }
    this.updateUserCoin(this.coinValue);
  }
  private updateUserCoin(diffCoin: number) {
    updateUserGoldCoin(databus.selfInfo.userId, databus.selfInfo.goldCoin, diffCoin);
    databus.selfInfo.addGoldCoin(diffCoin);

    // 获取金币的声音
    const sound = this.node.getChildByName('Sound').getChildByName('GainGoldCoin').getComponent(AudioSource);
    sound.volume = 1;
    sound.play();
    const that = this;
    setTimeout(() => {
      that.closeVideoCoin();
    }, 1000);
  }

  private updateEarnCoin() {
    updateSetting(databus.selfInfo.userId, databus.selfInfo.userId, 1, 'coin', JSON.stringify(databus.earnCoin));
  }

  private changeCoin() {
    this.node.getChildByName('AdSwitch').active = false;
    this.freeButton.node.active = true;
    this.doubleButton.node.active = true;
    this.tripleButton.node.active = true;
    this.normalButton.node.active = false;
    this.coinValue = 3000;
    if (this.adType === 1) { // 签到 3倍金币
      this.freeButton.node.active = false;
      this.doubleButton.node.active = false;
      if (this.isAdDoubled) {
        this.coinValue = 9000;
      }
    } else if (this.adType === 2) { // 每日福利 2倍金币
      this.freeButton.node.active = false;
      this.tripleButton.node.active = false;
      if (this.isAdDoubled) {
        this.coinValue = 6000;
      }
    } else {
      this.doubleButton.node.active = false;
      this.tripleButton.node.active = false;
      this.coinValue = 3000;
    }
  }
}